package com.tencent.org.controller;

import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.PageList;
import com.tencent.org.domain.Detail;
import com.tencent.org.query.DetailQuery;
import com.tencent.org.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController // 相当于controler + responsbody
@RequestMapping("/detail")
public class DetailController {

    @Autowired
    private DetailService detailService;


    @GetMapping
    public List<Detail> findAll(){
        return detailService.findAll();
    }


    @GetMapping("/{id}")
    public Detail findById(@PathVariable("id")Long id){
        return detailService.findById(id);
    }


    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            detailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }


    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        try {
            if (detail.getId() == null){
                detailService.add(detail);
                return new AjaxResult();
            }else {
                detailService.update(detail);
                return new AjaxResult();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }


    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery detailQuery){
        return detailService.queryPage(detailQuery);
    }


    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            detailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }
}
